import { Request, Response, NextFunction } from 'express';
import { logger } from '../config/logger';

export const performanceMonitor = (req: Request, res: Response, next: NextFunction) => {
  const start = Date.now();

  // 监听响应完成事件
  res.on('finish', () => {
    const duration = Date.now() - start;
    logger.info(`Request ${req.method} ${req.originalUrl} took ${duration}ms`);

    // 可以根据需要记录慢请求
    if (duration > 1000) {
      logger.warn(`Slow request detected: ${req.method} ${req.originalUrl} took ${duration}ms`);
    }
  });

  next();
};